﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq;

namespace BySoft.Web.Controls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:ErrorMessager runat=server></{0}:ErrorMessager>")]
    public class ErrorMessager : OneSoft.Web.UI.Compatibility.ValidationSummary, INamingContainer
    {

        protected override void OnInit(EventArgs e)
        {
            this.EnableClientScript = true;
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            this.CssClass = "hidden";
            base.OnLoad(e);
            string scriptTemp = @"<script type=""text/javascript"">
<!--
var Page_ValidationActive = false;
var currentGroup;
if (typeof(ValidatorOnLoad) == ""function"") {
    ValidatorOnLoad();
}

function ValidatorOnSubmit() { 
       if (Page_ValidationActive) {
      var result= ValidatorCommonOnSubmit();
 	        if(!result) {
                
                var currentGroupID;
                if(typeof(currentGroup)!=""undefined""&&currentGroup!="""")
                { 
                    for(i=0;i<Page_ValidationSummaries.length;i++)
                    {
	                        if(currentGroup==Page_ValidationSummaries[i].id.substr(Page_ValidationSummaries[i].id.length-currentGroup.length,currentGroup.length))
	                        {
                                currentGroupID=Page_ValidationSummaries[i].id;
                                break;
	                        }
                    }
                }
               if(typeof(currentGroupID)==""undefined""){
                      if(Page_ValidationSummaries.length>0)
                        {
                           currentGroupID=Page_ValidationSummaries[0].id;     
                        }
                    }                    
                ShowErrorMsg(""提示信息"",currentGroupID);
               
              

            return result;
             }
             else {
            return true;
             }
    }
}
 function Page_ClientValidate(validationGroup) {
    Page_InvalidControlToBeFocused = null;
    if (typeof(Page_Validators) =='undefined') {
        return true;
    }
    var i;
    for (i = 0; i < Page_Validators.length; i++) {
        ValidatorValidate(Page_Validators[i], validationGroup, null);
    }
    ValidatorUpdateIsValid();
    ValidationSummaryOnSubmit(validationGroup);
    Page_BlockSubmit = !Page_IsValid;
    currentGroup=validationGroup;
    return Page_IsValid;
 }  
// -->
</script>";
            ScriptManager.RegisterStartupScript(this, typeof(BaseValidator), "ValidatorIncludeScript", scriptTemp, false);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.RenderBeginTag(HtmlTextWriterTag.Center);
            base.Render(writer);
            writer.RenderEndTag();
        }

        private void AddError()
        {
            throw new Exception("The method or operation is not implemented.");
        }
    }
}
